package com.rainner.admin.util.timeutil;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.rainner.admin.javabean.BrrowedBookBean;
import com.rainner.admin.server.BrrowedBookServer;
import com.rainner.admin.server.ReaderServer;

public class EditReaderMoney {
	private Date date = null;
	private BrrowedBookServer server = new BrrowedBookServer();
	private ReaderServer readerServer = new ReaderServer();

	private EditReaderMoney() {
	}

	private static EditReaderMoney edit = null;

	public static EditReaderMoney getEditReaderMoney() {
		if (edit == null) {
			edit = new EditReaderMoney();
		}
		return edit;
	}

	@SuppressWarnings("deprecation")
	public void editReaderMoney() throws SQLException, ParseException {
		Calendar cal = Calendar.getInstance();
		date = new Date(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
		// 计算当前日期

		List<BrrowedBookBean> data = server.allData();
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		for (BrrowedBookBean brrowedBookBean : data) {
			if (compareDate(date,formatDate.parse(brrowedBookBean.getBorrowedDate()) )) {
				readerServer.updateReaderMoney(brrowedBookBean.getReaderUsername(), 0 - 0.1 * brrowedBookBean.getBorrowedBookNumber());
			}
		}
	}

	/**
	 * 比较今天的日期是否比数据库中的日期大30天
	 * 
	 * @param today今天的日期
	 * @param date数据库中的日期
	 * @return true 过期,false 未过期
	 */
	private boolean compareDate(Date today, Date date) {
		Date d = new Date(date.getTime() + 30 * 24 * 60 * 60 * 1000);
		if (d.after(today)) {
			return false;
		} else {
			return true;
		}
	}
}
